package com.example.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.student.config.PageParam;
import com.example.student.entity.Student;
import com.example.student.mapper.StudentMapper;
import com.example.student.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

        @Resource
        private StudentMapper mapper;

        /**
        * 根据id获取单条
        * @param id
        * @author lx
        * @return
        */
        @Override
        public Student getOne (String id){
                return mapper.selectById(id);
        }

        /**
        * 新增
        * @param student
        * @author lx
        * @return
        */
        @Override
        public boolean add (Student student){
                return mapper.insert(student)>0;
        }

        /**
        * 修改
        * @param student
        * @author lx
        * @return
        */
        @Override
        public boolean update (Student student){
                return mapper.updateById(student)>0;
        }

        /**
        * 删除
        * @param id
        * @author lx
        * @return
        */
        @Override
        public boolean delete(String id){
                return mapper.deleteById(id)>0;
        }

        /**
        * 批量删除
        * @param ids
        * @author lx
        * @return
        */
        @Override
        public boolean deleteByIds(String[] ids){
                for(String id:ids){
                    mapper.deleteById(id);
                }
                return true;
        }

        /**
        * 分页查询
        * @param page
        * @author lx
        * @return
        */
        @Override
        public IPage<Student> page(PageParam page){
            return mapper.page(new Page(page.getPage(),page.getSize()),page);
        }

        /**
         * 根据手机号获取信息
         * @param num
         * @return
         */
        @Override
        public Student getByNum(String num){
                return this.mapper.selectOne(new QueryWrapper<Student>().eq("num",num));
        }


        /**
         * 密码重置
         * @param ids
         * @author lx
         * @date 2021/1/27 16:54
         * @return
         */
        @Override
        public boolean restPassword(String[]  ids){
                for(String id:ids){
                        Student user = mapper.selectById(id);
                        user.setPassword("123456");
                        mapper.updateById(user);
                }
                return true;
        }

        /**
         * 图表统计接口
         * @return
         */
        @Override
        public Map<String,Object> report() {
                Map<String,Object> data = new HashMap<>();
                data.put("faculty",mapper.facultyGroup());
                data.put("major",mapper.majorGroup());
                data.put("sex",mapper.sexGroup());
                return data;
        }

}
